<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\FileUpload;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Carousel;
use app\BaseController;
use think\facade\Env;
use think\Request;

class CarouselCon extends BaseController
{
    use FileUpload;


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 8:49
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源
     */
    public function getCarouselList()
    {
        $limit = $this->request->param('limit', 10);
        $res = Carousel::order(['create_time' => 'desc'])->paginate($limit)->each(function ($item, $key) {
            $item['url'] = Env::get('IMG_HTTP.img_http').$item['url'];
            return $item;
        });
        return $this->response($res);
    }


    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2021/12/27
     * Time: 20:36
     * @return \think\response\Json
     * 上传照片
     */
    public function UploadImg()
    {
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');

        $result = $this->save($file, 'carousel');
        return $this->response($result);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 8:38
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存轮播图
     */
    public function saveCarousel()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['uid'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            Carousel::create($data);
        } else {
            $res = Carousel::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增用户' : '编辑用户', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 9:37
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除
     */
    public function deleteCarousel($id)
    {
        $res = Carousel::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        if (!empty($res->url)) {
            $file = app()->getRootPath() . 'public/storage/' . $res->url;
            if (file_exists($file)) {
                unlink($file);
            }
        }
        $res->force()->delete();
        AdminLog::record('删除幻灯片', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }
}
